package io.github.loan.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import io.github.loan.common.controller.BaseController;
import io.github.loan.common.util.PageInfoUtil;
import io.github.loan.users.pojo.Admins;
import io.github.loan.users.service.IUsersService;

/**
 * 登陆后的操作
 * @author Andone
 *
 */
@Controller
@RequestMapping("/back/admins/")
public class AdminsBackController extends BaseController{
	
	@Resource
	private IUsersService usersService;
	/**
	 * 登陆后的首页
	 */
	@RequestMapping("/index.mvc")
	public String index(){
		return "/back/index";
	}
	
	
	/**
	 * 退出
	 */
	@RequestMapping("/logout.mvc")
	public String logout(HttpServletRequest request){
		/**
		 * 登录的时候放入session的信息,remove
		 */
		HttpSession session=request.getSession();
		session.removeAttribute("admins");
		session.removeAttribute("lastLoginTime");
		/*退出客户端跳转*/
		return "redirect:/adminsLogin.mvc";
	}
	
	/**
	 * 左侧菜单
	 */
	@RequestMapping("/leftMenu.mvc")
	public String leftMenu(){
		return "/back/leftMenu";
	}
	
	/**
	 * 管理员列表
	 */
	@RequestMapping("/adminsList.mvc")
	public String adminsList(HttpServletRequest request){
		
		//接收参数
		String keyword=request.getParameter("keyword");
		String status=request.getParameter("status");
		String st=request.getParameter("st");
		String ed=request.getParameter("ed");
		
		if(st==null){
			st="";
		}
		if(ed==null){
			ed="";
		}
		Date stDate=null;
		Date edDate=null;
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}
		PageInfoUtil pageInfoUtil=this.proccedPageInfo(request);
		/*查询数据*/
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		List<Admins> adminsList=this.usersService.findAdminsCondListService(pageInfoUtil, condMap);
		/*存放结果*/
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/*存储条件*/
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return "/back/adminsList";
	}
	/**
	 * 管理员添加的页面
	 */
	@RequestMapping("/adminsInsert.mvc")
	public String adminsInsert(){
		return "/back/adminsInsert";
	}
	/**
	 * 管理员添加提交页面
	 * 
	 * produces="text/html;charset=UTF-8"==
	 * response.setContextType("text/html;charset=UTF-8");
	 * @return
	 */
	@RequestMapping(value="/adminsInsertSubmit.mvc",produces="text/html;charset=UTF-8")
	public @ResponseBody String adminsInsertSubmit(HttpServletRequest request,Admins admins){
		System.out.println(admins);
		admins.setPassword(this.encryptUtil.encryptString(admins.getPassword()));
		admins.setCreateTime(new Date());
		admins.setUpdateTime(new Date());
		admins.setLastLoginTime(new Date());
		JSONObject resultJSON=this.usersService.insertOneAdminsService(admins);
		resultJSON=this.returnJuiJSON(request, resultJSON.getString("info"));
		/* 返回JUI需要的JSON
		 * @ResponseBody:默认返回字符串
		 *  */
		return resultJSON.toJSONString();
	}
}
